# -*- coding: utf-8 -*-
import qqlib
import requests
import sys
import json
import os,glob
os.system('modprobe w1_gpio')
os.system('modprobe w1_therm')

base_dir='/sys/bus/w1/devices/'
device_folder=glob.glob(base_dir+'28*')[0]
device_file=device_folder+'/w1_slave'

f=open(device_file,'r')
lines=f.readlines()
f.close
#lines = read_temp_raw()
while lines[0].strip()[-3:]!='YES':
  time.sleep(0.2)
#  lines=read_temp_raw
equals_pos=lines[1].find('t=')
if equals_pos !=-1:
  temp_string=lines[1][equals_pos+2:]
  temp_c=float(temp_string)/1000.0
  temp_olddata1=open('/home/pi/yeelink/qw/temp_data_qzone.txt','r')
  old_temp=temp_olddata1.read()
  temp_diff=round(temp_c-float(old_temp[9:18]),2)
  temp_olddata1.close
  temp_olddata = open('/home/pi/yeelink/qw/temp_data_qzone.txt','w')
  res = '{"value":%f}'%temp_c
  temp_olddata.write('%s'%res)
  temp_olddata.close
  print ("diff is:%s"%temp_diff)
  print("temp C=%f"%temp_c)

if temp_diff>0:
  qzone_string="现在温度为：%s"%temp_c+"℃,"+"比上一次测量的温度高%s"%abs(temp_diff)+"℃。\n"+"   "+"--By Raspberry pi 3 mode B"
else:
  qzone_string="现在温度为：%s"%temp_c+"℃,"+"比上一次测量的温度低%s"%abs(temp_diff)+"℃。\n"+"   "+"--By Raspberry pi 3 mode B"


qq = qqlib.QQ(410363916, 'HSX407045494')
try:
  qq.login()
except qqlib.NeedVerifyCode as e:
  verifier = e.verifier
  open('verify.jpg', 'wb').write(verifier.image)
  print('TMD的要验证码，别折腾了。verify.jpg')
  vcode = input('请输入验证码：')
  try:
    kw = verifier.verify(vcode)
  except qqlib.VerifyCodeError as e:
    print('验证码错误！')
    raise e
  else:
    qq.login()
    print("登录成功！")
print('hello world')
qq.feed(qzone_string)
print("发送成功！")
